package com.example.express.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.BaseRequestOptions;
import com.bumptech.glide.request.RequestOptions;
import com.example.express.R;
import com.example.express.entity.KuaiDi;

import java.util.List;

public class QueryAdapter extends RecyclerView.Adapter<QueryAdapter.ViewHolder> {
    private List<KuaiDi> kuaiDis;
    private QueryAdapterItemClick itemClick;
    private Context context;

    public QueryAdapter(List<KuaiDi> kuaiDis, QueryAdapterItemClick itemClick) {
        this.kuaiDis = kuaiDis;
        this.itemClick = itemClick;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context)
                .inflate(R.layout.item_kuaidi, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        KuaiDi kuaiDi = kuaiDis.get(position);
        holder.itemView.setOnClickListener(v -> itemClick.item(kuaiDi.url,kuaiDi.name));
        holder.phoneView.setOnClickListener(v -> itemClick.itemPhone(kuaiDi.phone));
        holder.phoneView.setText(kuaiDi.phone);
        Glide.with(context)
                .load(kuaiDi.img)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(holder.imageView);
      holder.nameView.setText(kuaiDi.name);
    }

    @Override
    public int getItemCount() {
        return kuaiDis==null ? 0 : kuaiDis.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView nameView;
        private TextView phoneView;
        private ImageView imageView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            nameView = itemView.findViewById(R.id.name);
            phoneView = itemView.findViewById(R.id.phone);
            imageView = itemView.findViewById(R.id.image);
        }
    }

    public interface QueryAdapterItemClick{
        /**
         * 子项点击事件
         * @param url 快递查询链接
         * @param title 标题
         */
        void item(String url,String title);
        /**
         * 子项电话点击事件
         * @param phone 快递公司电话号码
         */
        void itemPhone(String phone);
    }
}
